home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8529 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem Negating an Unsigned Char
  5. Date: 4 Mar 1996 13:18:08 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hfmmgINN4t8@anvil.ugrad.cs.ubc.ca>
  8. References: <Dnnros.Lq.0.-s@hkusuc.hku.hk> <4he27sINNdel@keats.ugrad.cs.ubc.ca> <DnqMyr.4pF@cwi.nl>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <DnqMyr.4pF@cwi.nl>, Dik T. Winter <dik@cwi.nl> wrote:
  12.  >In article <4he27sINNdel@keats.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  13.  >...
  14.  > > >unsigned char a=0x11;
  15.  > > >unsigned char b=0xEE;
  16.  >...
  17.  > > >    if( a == ~b ) {
  18.  >...
  19.  > > >The c remains unchange, while it changes to 1 if I cast the ~b to unsigned
  20.  > > >char as if( a == (unsigned char) ~b )
  21.  > > 
  22.  > > The cast forces the integer value of ~b into an unsigned char, stripping
  23.  > > high-order bits. The above will work only on machines with eight bit chars.
  24.  > > 
  25.  > > To make it portable, you must manually mask for the lower eight bits:
  26.  > > 
  27.  > >     if (a == (~b & 0xff))
  28.  >
  29.  >And how would this work on machines with other than eight bit chars?
  30.  
  31. Just fine, thank you. The C standard guarantees char to be at least eight bits
  32. wide. The original poster wants eight bit arithmetic; i.e. that the complement 
  33. of 0x11 be equal to 0xEE. This is what he is testing for, and the above is how 
  34. you get it.
  35.  
  36.  >Casting to unsigned char will work regardless the number of bits in a char.
  37.  
  38. Casting to an unsigned quantity is implementation defined. Are you suggesting
  39. that the invocation of implementation defined behavior is more portable than a
  40. well-defined bit masking operation? You haven't read your K&R2 appendices, have
  41. you? Section A6.1 states that an integral type (i.e. signed or unsigned char or 
  42. bitfield) is promoted to an int, if it can be represented by an int, or to an 
  43. unsigned int otherwise. If it gets promoted to int, and the machine you are on 
  44. is not two's complement, then a cast back to an unsigned, narrower type may not
  45. produce the right quantity.
  46.  
  47. Suppose that the machine is sign and magnitude, and ints are 16 bits, whereas
  48. unsigned chars are 8 bits. Your 0xEE value gets promoted to an int: 
  49. 0000 0000 1110 1110.  Now, you take the complement: 1111 1111 0001 0001.
  50. This is just the sign + magnitude representation of -0111 1111 0001 0001, in
  51. other words -32529.
  52.  
  53. When you cast that to the narrower unsigned char type, the result is the least 
  54. positive residue of that value modulo 2^8. (K&R 6.1A). This is just 239.
  55.  
  56. 239 in hex is 0xEF
  57.  
  58. it is not 0xEE.
  59.  
  60. Do you still think that a cast will work regardless of the number of bits in a
  61. char?
  62. -- 
  63.  
  64.